package lib.bone.admin.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.system.module.log.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("system")
@Api(tags = "日志管理")
public class SysLogController {

    @Autowired
    SysLogService sysLogService;

    @GetMapping("logs")
    @ApiOperation(value = "查询全部",notes = "")
    public ResponseDTO<Object> getAll(SysLogQueryDTO sysLogQueryDTO){
        Sort sort;
        if(sysLogQueryDTO.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, sysLogQueryDTO.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, sysLogQueryDTO.getSortBy());
        }

        Pageable pageable = PageRequest.of(sysLogQueryDTO.getPageNum() - 1 , sysLogQueryDTO.getPageSize(), sort);

        SysLog sysLog = new SysLog();
        BeanUtil.copyProperties(sysLogQueryDTO,sysLog, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<SysLog> example = Example.of(sysLog);
        return ResponseDTO.succData(sysLogService.getAll(pageable,example));
    }

    @WebLog(module = "bone-system")
    @PostMapping(value = "logs")
    @ApiOperation(value = "新建记录",notes = "")
    public ResponseDTO saveOne(@RequestBody @Validated SysLogAddDTO SysLogAddDTO){
        SysLog sysLog = new SysLog();
        BeanUtil.copyProperties(SysLogAddDTO,sysLog,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(sysLogService.saveOne(sysLog));
    }

    @GetMapping("logs/{id}")
    @ApiOperation(value = "查询单条记录",notes = "传入ID")
    public ResponseDTO getOne(@PathVariable("id") Long id){
        Optional<SysLog> sysLogOp = sysLogService.getById(id);
        return sysLogOp.<ResponseDTO>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "ID:[" + id + "]对应的数据不存在"));
    }

    @WebLog(module = "bone-system",operate = MethodTypeConst.UPDATE)
    @PutMapping("logs")
    @ApiOperation(value = "更新单条记录",notes = "")
    public ResponseDTO<Object> updateOne(@RequestBody @Validated SysLogUpdateDTO sysLogUpdateDTO){
        Optional<SysLog> sysLogOp = sysLogService.getById(sysLogUpdateDTO.getId());
        if (!sysLogOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        }
        SysLog sysLog = (SysLog) sysLogOp.get();
        BeanUtil.copyProperties(sysLogUpdateDTO,sysLog,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(sysLogService.updateOne(sysLog));
    }

    @WebLog(module = "bone-system",operate = MethodTypeConst.DELETE)
    @DeleteMapping("logs/{id}")
    @ApiOperation(value = "删除单条记录",notes = "传入ID")
    public ResponseDTO<Object> deleteOne(@PathVariable("id") Long id){
        return ResponseDTO.succData(sysLogService.deleteById(id));
    }

}
